Repository navigation
Trim cgp::prelude to the names users write - #294
Merged
Merged
Conversation
`cgp::macro_prelude` was an alias of the prelude, so every name the macros' generated code refers to had to be glob-imported into user modules. It is now a module of its own in `cgp`, `cgp-core`, and `cgp-extra`, holding the full previous set, and the prelude keeps only the macros, derives, traits, providers, type-level types, and handler traits and keys that users write. The dropped names stay importable from their modules: the wiring machinery (`IsProviderFor`, `DelegateComponent`, `CanUseComponent`, `RedirectLookup`) from `cgp::core::component`, the path, field, and extractor traits and markers from `cgp::core::field` and `cgp::core::base`, `HasType` and `TypeProvider` from `cgp::core::types`, and the reference handler forms, `Promote*` adapters, and value-handler matchers from `cgp::extra::handler` and `cgp::extra::dispatch`. A derive keeps its name in the prelude where its trait leaves, as `ExtractField` and `FromVariant` do, and `Symbol!` stays while the `Symbol` type leaves. This is a breaking change for code that used a dropped name through the glob. The crates and tests here import those names from their modules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler macros reach every name they emit through `cgp::macro_prelude`, so the prelude no longer exports `cgp_handler`'s traits and keys: `Computer`, `AsyncComputer`, `TryComputer`, `Handler`, `Producer`, and their component keys. Code that writes a provider or a wiring entry for them imports them from `cgp::extra::handler`, as code that calls a handler already did for the consumer traits. `UseContext` leaves the prelude too and stays in `cgp::core::component`. The provider struct's generated doc linked its trait by the last path segment, which resolved only while the prelude put the handler traits in scope. A multi-segment trait path now links its full path, so `#[cgp_computer]` output links `cgp::macro_prelude::Computer` wherever it expands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`cgp::prelude` no longer re-exports `UseField`, `BuildField`, `FinalizeBuild`, `HasBuilder`, `HasField`, `HasFields`, `Cons`, `Either`, `Field`, `Index`, `Life`, `Nil`, or `Void`. The macros reach them through `cgp::macro_prelude`, so most code never names them; code that does imports them from `cgp::core::field::impls`, `traits`, or `types`. The derives of the same names stay in the prelude. The tests import the names they use, and the test modules left with no use of the glob drop it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each `macro_prelude` holds the names the macros' generated code refers to. Its doc comment now says that only the macros use it and points readers to `cgp::prelude` or the module that defines a name, since rustc's import hints and docs.rs both show the module to users. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
[claude-opus-5-5]
use cgp::prelude::*now brings in only the names users write: the macros and derives, the error traitsHasErrorType,CanRaiseError, andCanWrapError, theUseTypeandUseDelegateproviders,DefaultNamespace, andPhantomData. Before this PR,cgp::macro_preludewas an alias of the prelude, so every name the generated code refers to, such asIsProviderFor,DelegateComponent, the field traits,HasType, and the handler traits, came into user modules with the glob.cgp::macro_preludeis now a module of its own incgp,cgp-core, andcgp-extra, holding the full previous set, so every path the macros generate still resolves. Its doc comment says that only the macros use it and sends readers tocgp::preludeor the module that defines a name, since rustc's import hints can suggest it.Impact
This is a breaking change for code that used a dropped name through the glob. Each dropped name stays importable from its module:
IsProviderFor,DelegateComponent,CanUseComponent,RedirectLookup,UseContext,UseFields,WithContext, andWithProvider, fromcgp::core::component.HasField,HasFields,HasBuilder, andBuildField, fromcgp::core::field::traits;UseFieldand the field-mode markers such asIsPresentandIsRef, fromcgp::core::field::impls; the type-level typesCons,Nil,Either,Void,Field,Index, andLife, withChars,MRef, andSymbol, fromcgp::core::field::types; andPathConsandConcatPath, fromcgp::core::base::typesandcgp::core::base::traits.HasTypeandTypeProvider, fromcgp::core::types.ComputerandHandler, their component keys, and the promotion bundles such asPromoteComputer, fromcgp::extra::handler. The prelude keeps only the#[cgp_computer],#[cgp_producer], and#[cgp_auto_dispatch]macros.cgp::extra::dispatch.Derives and type-level macros stay in the prelude where their traits and types leave.
#[derive(HasField)],HasFields,BuildField,ExtractField, andFromVariantwork without an import, while a bound on one of those traits or a call to its methods needs one, andProduct!,Sum!, andSymbol!stay while the types they expand to leave.A provider written with
#[cgp_impl]onComputerimportsComputerComponentas well asComputer. The macro names the key as{Trait}Componentin the caller's scope.Plain
cargo checkoutput qualifies a dropped name throughcgp::macro_prelude. When a module does not import the name, rustc writescgp::macro_prelude::HasFieldin place of the earliercgp::prelude::HasField, which is why the dependents' compile-fail snapshots change.The dependent repositories pass CI only once this PR merges.
cgp-examples,cgp-serde, Hypershell, the website's example code, andcargo-cgp's fixtures import what they lose and expect the trimmed prelude in their snapshots, as the crates and tests in this repository do. Hypershell's own prelude exports the handler traits and keys, since its programs are handler pipelines, and its modules importConsandNilwhere they write them.The doc that
#[cgp_impl(new …)]and#[cgp_new_provider]give a provider struct now links its trait by the full path. It linked the last path segment, which resolved only while the prelude put the handler traits in scope. A#[cgp_computer]provider's doc now linkscgp::macro_prelude::Computer, which rustdoc resolves to the trait's own page, andcargo docruns without broken-link warnings.Areas needing attention
The keep list in
crates/main/cgp-core/src/prelude.rsandcrates/main/cgp-extra/src/prelude.rsneeds the most judgment. A name it leaves out that users commonly write would break code the docs show. The doc-link change is incrates/macros/cgp-macro-core/src/types/cgp_provider/item.rs.